package ru.compscicenter.uno.view;

import ru.compscicenter.uno.model.Card;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by IntelliJ IDEA.
 * User: Павел
 * Date: 10.12.11
 * Time: 0:32
 * To change this template use File | Settings | File Templates.
 */
public class SelectColorDialog extends JDialog {
    private final JPanel red = new JPanel();
    private final JPanel blue = new JPanel();
    private final JPanel yellow = new JPanel();
    private final JPanel green = new JPanel();
    private static Card.Color value = null;

    public static Card.Color showDialog(Component frameComp,
                                        Component locationComp,
                                        String title) {
        Frame frame = JOptionPane.getFrameForComponent(frameComp);
        SelectColorDialog dialog = new SelectColorDialog(frame,
                locationComp,
                title);

        dialog.setLocation(frame.getX() + 250, frame.getY() + 50);
        dialog.setSize(300, 500);

        int w = dialog.getWidth();
        int h = dialog.getHeight();
        dialog.red.setBounds(0, 0, w / 2, h / 2);
        dialog.blue.setBounds(w / 2, 0, w / 2, h / 2);
        dialog.yellow.setBounds(0, h / 2, w / 2, h / 2);
        dialog.green.setBounds(w / 2, h / 2, w / 2, h / 2);
        dialog.setVisible(true);
        return value;
    }

    private SelectColorDialog(Frame frame,
                             Component locationComp,
                             String title) {
        super(frame, title, true);
        this.setResizable(false);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        red.setBackground(new Color(0xEE1C23));
        blue.setBackground(new Color(0x0095DA));
        yellow.setBackground(new Color(0xFECB09));
        green.setBackground(new Color(0x00A54F));

        add(red);
        add(blue);
        add(yellow);
        add(green);

        this.setTitle(title);

        red.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                value = Card.Color.RED;
                setVisible(false);
            }
        });
        blue.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                value = Card.Color.BLUE;
                setVisible(false);
            }
        });
        yellow.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                value = Card.Color.YELLOW;
                setVisible(false);
            }
        });
        green.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                value = Card.Color.GREEN;
                setVisible(false);
            }
        });

        //Initialize values.
        pack();
        setLocationRelativeTo(locationComp);
    }
}

